package com.hxd.mall.widget.refresh

import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.hxd.mall.R
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshKernel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.constant.SpinnerStyle
import kotlinx.android.synthetic.main.refresh_head.view.*


class HXRefreshHeader : LinearLayout, RefreshHeader {

    init {
        LayoutInflater.from(context).inflate(R.layout.refresh_head, this)
        setBackgroundResource(R.color.transparent)
    }

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}


    override fun getView(): View {
        return this
    }

    override fun getSpinnerStyle(): SpinnerStyle {
        return SpinnerStyle.Translate
    }

    override fun setPrimaryColors(vararg colors: Int) {

    }

    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {
    }

    override fun onMoving(isDragging: Boolean, percent: Float, offset: Int, height: Int, maxDragHeight: Int) {
//        pull_progressbar.updateView(((percent - 0.5) * 800).toInt())
        tv_progress.text = "下拉后自动刷新  "
    }

    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        return 0
    }

    override fun onHorizontalDrag(percentX: Float, offsetX: Int, offsetMax: Int) {

    }

    override fun isSupportHorizontalDrag(): Boolean {
        return false
    }

    override fun onStateChanged(refreshLayout: RefreshLayout, oldState: RefreshState, newState: RefreshState) {
        when (newState) {
            RefreshState.None, RefreshState.PullDownToRefresh -> {
            }
            RefreshState.Refreshing -> {
                startFloatAnimator()
                tv_progress.text = context.getString(R.string.refresh_hint)
            }
            RefreshState.ReleaseToRefresh -> {
            }
            RefreshState.RefreshFinish -> {
                animator?.cancel()
//                pull_progressbar?.animate()?.translationY(0f)?.start()
                iv_progress.clearAnimation()
                tv_progress.text = "刷新完成  "
            }
            else -> {
            }
        }
    }

    //    var animator: ViewPropertyAnimator? = null
    var animator: ObjectAnimator? = null

    fun startFloatAnimator() {
//        animator = pull_progressbar.animate().translationY(5.0f).setListener(object : Animator.AnimatorListener {
//        animator = iv_progress.animate().rotation(360f).setListener(object : Animator.AnimatorListener {
//            override fun onAnimationRepeat(animation: Animator?) {
//
//            }
//
//            override fun onAnimationEnd(animation: Animator?) {
////                animator = pull_progressbar?.animate()?.translationY(-5.0f)?.setListener(object : Animator.AnimatorListener {
//                animator = iv_progress?.animate()?.rotation(360f)?.setListener(object : Animator.AnimatorListener {
//                    override fun onAnimationRepeat(animation: Animator?) {
//
//                    }
//
//                    override fun onAnimationEnd(animation: Animator?) {
//                        startFloatAnimator()
//                    }
//
//                    override fun onAnimationCancel(animation: Animator?) {
//
//                    }
//
//                    override fun onAnimationStart(animation: Animator?) {
//
//                    }
//
//                })?.setDuration(300L)
//                //animator?.start()
//            }
//
//            override fun onAnimationCancel(animation: Animator?) {
//
//            }
//
//            override fun onAnimationStart(animation: Animator?) {
//
//            }
//
//        }).setDuration(300L)
//        animator?.start()

        //animator = iv_progress.animate().rotation(360f).setDuration(2000L)

        animator = ObjectAnimator.ofFloat(iv_progress, "rotation", 360f)
        animator?.repeatMode = ObjectAnimator.RESTART
        animator?.duration = 200
        animator?.start()
    }
}
